package com.visualexpress.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.visualexpress.domain.Location;
import com.visualexpress.domain.Shipment;
import com.visualexpress.domain.TimeLocation;
import com.visualexpress.domain.User;
import com.visualexpress.domain.UserShipment;

public class MessageDAO extends BaseHibernateDAO{
	private static final Log log = LogFactory.getLog(MessageDAO.class);
	
	public ArrayList<Location> getLocations(Shipment shipment){
		TimeLocationDAO tldao = new TimeLocationDAO();
		List listtl = (List)tldao.findByShipment(shipment);
		if(listtl.isEmpty()){
			log.debug("getLocations failed,no shipment matched in database");
			return null;
		}
		ArrayList<Location> listLocations = new ArrayList<Location>(listtl.size());
		for(int i = 0; i < listtl.size(); i++){
			listLocations.add(((TimeLocation)listtl.get(i)).getLocation()) ;			
		}
		
		return listLocations;
	}
	
	public ArrayList<Date> getArrivalTimes(Shipment shipment){
		TimeLocationDAO tldao = new TimeLocationDAO();
		List listtl = (List)tldao.findByShipment(shipment);
		if(listtl.isEmpty()){
			log.debug("getLocations failed,no shipment matched in database");
			return null;
		}
		ArrayList<Date> listDates = new ArrayList<Date>(listtl.size());
		for(int i = 0; i < listtl.size(); i++){
			listDates.add(((TimeLocation)listtl.get(i)).getArrivalTime()) ;			
		}
		
		return listDates;
	} 
	
	public ArrayList<Date> getLeaveTimes(Shipment shipment){
		TimeLocationDAO tldao = new TimeLocationDAO();
		List listtl = (List)tldao.findByShipment(shipment);
		if(listtl.isEmpty()){
			log.debug("getLocations failed,no shipment matched in database");
			return null;
		}
		ArrayList<Date> listDates = new ArrayList<Date>(listtl.size());
		for(int i = 0; i < listtl.size(); i++){
			listDates.add(((TimeLocation)listtl.get(i)).getLeaveTime()) ;			
		}
		
		return listDates;
	} 
	
	public ArrayList<User> getAllRelatedUsers(Shipment shipment){
		UserShipmentDAO tldao = new UserShipmentDAO();
		List listtl = (List)tldao.findByShipment(shipment);
		if(listtl.isEmpty()){
			log.debug("getLocations failed,no shipment matched in database");
			return null;
		}
		ArrayList<User> listUsers = new ArrayList<User>(listtl.size());
		for(int i = 0; i < listtl.size(); i++){
			listUsers.add(((UserShipment)listtl.get(i)).getUser()) ;			
		}
		
		return listUsers;
	}
}
